/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package icexam.dao;

import icexam.exam.Issue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Ethan
 */
public class IssueDao {

    public ArrayList<Issue> getAllIssue(int type){
        ArrayList<Issue> issues = new ArrayList<Issue>();
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("select * from issue where questiontype=? order by random()");
            statement.setInt(1, type);
            ResultSet rs = statement.executeQuery();
            String[] tmp = null;
            while(rs.next()){
                Issue iss = new Issue();
                iss.setQid(rs.getInt(1));
                iss.setIssue(rs.getString(2));
                iss.setQuestionType(rs.getInt(3));
                tmp = new String[2];
                tmp[0] = rs.getString(4);
                tmp[1] = rs.getString(5);
                iss.setAnswers(tmp);
                iss.setStandardAnswerIndex(rs.getInt(6));
                issues.add(iss);
            }
            
        } catch (SQLException ex) {
           ex.printStackTrace();
        }
        return issues;
    }
    public ArrayList<Issue> getAllIssue(){
        ArrayList<Issue> issues = new ArrayList<Issue>();
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("select * from issue order by qid");
            ResultSet rs = statement.executeQuery();
            String[] tmp = null;
            while(rs.next()){
                Issue iss = new Issue();
                iss.setQid(rs.getInt(1));
                iss.setIssue(rs.getString(2));
                iss.setQuestionType(rs.getInt(3));
                tmp = new String[2];
                tmp[0] = rs.getString(4);
                tmp[1] = rs.getString(5);
                iss.setAnswers(tmp);
                iss.setStandardAnswerIndex(rs.getInt(6));
                issues.add(iss);
            }

        } catch (SQLException ex) {
           ex.printStackTrace();
        }
        return issues;
    }
    public static void main(String[] args){
        ArrayList<Issue> is  = new IssueDao().getAllIssue(0);
        System.out.println(is.size());
        for(Issue i : is){
            System.out.println(i.getQid());
        }
    }
}
